package com.student.Activitys.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.adapter.EvaluateAdapter;
import com.student.Activitys.bean.InfoReturnBean;
import com.student.Activitys.bean.ScheduleTestingBean;
import com.student.Activitys.bean.ScheduleWeekBean;
import com.odd.Activitys.http.manager.ScheduleManager;
import com.student.Activitys.utils.Constant;
import com.student.Activitys.utils.ToastUtils;
import com.willy.ratingbar.ScaleRatingBar;
import com.zhy.autolayout.AutoRelativeLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：智慧校园学生端
 * 类描述：随堂评价 页面
 * 创建人：张飞祥
 * 创建时间：2017/9/30 11:24
 * 修改人：张飞祥
 * 修改时间：2017/9/30 11:24
 * 修改备注：
 */
public class EvaluateClassRoomActivity extends BaseActivity {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_evaluate_cr_teacher)
    TextView tvEvaluateCrTeacher;
    @Bind(R.id.tv_evaluate_cr_course)
    TextView tvEvaluateCrCourse;
    @Bind(R.id.tv_evaluate_cr_time)
    TextView tvEvaluateCrTime;
    @Bind(R.id.tv_evaluate_cr_place)
    TextView tvEvaluateCrPlace;
    @Bind(R.id.et_leave_reason)
    EditText etLeaveReason;
    @Bind(R.id.btn_evaluate_cr_submit)
    Button btnEvaluateCrSubmit;
    @Bind(R.id.rv_evaluate_list)
    RecyclerView rvEvaluateList;

    //传递过来的实体
    List<ScheduleTestingBean> list = null;

    //评价所需 星星
    private EvaluateAdapter adapter;
    //RecyclerView管理器
    LinearLayoutManager layoutManager = null;
    //传递过来的实体 bean
    public ScheduleWeekBean testingBean = null;
    //当前学年学期
    public String xnxq = "";
    //当前上课周次
    public String skzc = " ";

    //初始化网络请求
    public ScheduleManager mApiManager;
    //弹窗 diaolog
    public MaterialDialog promptDialog;
    @Override
    protected void initUI() {
        setContentView(R.layout.act_evaluate_class_room);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getScheduleApiManager(EvaluateClassRoomActivity.this, EvaluateClassRoomActivity.this.getTaskId());
    }

    @Override
    protected void initData() {
        //标题
        tvTitle.setText("随堂评价");
        //返回键
        ivReturn.setVisibility(View.VISIBLE);
        //管理器
        layoutManager = new LinearLayoutManager(EvaluateClassRoomActivity.this, LinearLayoutManager.VERTICAL, false);
        // 获取传递过来的封了数据的Bundle
        Bundle bundle = this.getIntent().getExtras();
        // 获取 传递过来的 实体 数据
        testingBean = bundle.getParcelable(Constant.SCHEDULE_WEEK_BEAN);
        xnxq = bundle.getString(Constant.XNXQ);
        skzc = bundle.getString(Constant.SKZC);
        list = (List<ScheduleTestingBean>) bundle.getSerializable(Constant.EVALUATE_JSON_PACK);
        //状态评价列表
        adapter = new EvaluateAdapter(EvaluateClassRoomActivity.this, list);

        rvEvaluateList.setLayoutManager(layoutManager);
        rvEvaluateList.setAdapter(adapter);
        //老师名字
        tvEvaluateCrTeacher.setText(testingBean.getTeacherName());
        //课程
        tvEvaluateCrCourse.setText(testingBean.getKCM());
        //时间
        tvEvaluateCrTime.setText(testingBean.getSKSJ());
        //地点
        tvEvaluateCrPlace.setText(testingBean.getSKDD());
    }

    @Override
    protected void getNotData() {

    }

    /**
     * 日课表 评价提交 加载是否成功
     */
    @Subscriber(tag = Constant.BUS_EVALUATE_ON_COMPLETED,mode = ThreadMode.MAIN)
    public void initTestingOnCompleted(String str) {
        //销毁handler
        time_handler.removeCallbacks(time_runnable);
        timeDailog.dismiss();
        dialog.dismiss();
    }

    /**
     * 日课表 评价提交 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_EVALUATE_DETAILS_LIST,mode = ThreadMode.MAIN)
    public void initTestingList(InfoReturnBean bean) {
        if (bean.getState().equals("1")){
            promptDialog = new MaterialDialog.Builder(EvaluateClassRoomActivity.this)
                    .title("成功")
                    .content(bean.getMsg())
                    .positiveText("确认")
                    .positiveColor(Color.GREEN)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            //刷新日课表
                            EventBus.getDefault().post("refresh", Constant.SCHEDULE_DAY_REFRESH);
                            finish();

                        }
                    }).show();
        }else{
            promptDialog = new MaterialDialog.Builder(EvaluateClassRoomActivity.this)
                    .title("失败")
                    .content(bean.getMsg())
                    .negativeText("重试")
                    .positiveText("确认")
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            initSubmit();
                        }
                    })
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            promptDialog.dismiss();
                        }
                    }).show();
        }
        // 设置点击屏幕Dialog不消失
        promptDialog.setCanceledOnTouchOutside(false);
        //取消Back键监听
        promptDialog.setCancelable(false);
    }

    /**
     * 所有点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_return, R.id.btn_evaluate_cr_submit})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:                 //退出
                finish();
                break;
            case R.id.btn_evaluate_cr_submit:    //提交
                initSubmit();
                break;
        }
    }

    //提交事件
    private void initSubmit() {
        String pjly=etLeaveReason.getText().toString();
        if (!pjly.equals("")){
            //显示 加载dialog
            initShowTimeDialog("正在提交您给出的评价，请稍后...",EvaluateClassRoomActivity.this);
            //开始网络请求
            mApiManager.evaluate(Constant.HTTP_EVALUATE_ACTION,Constant.STPJ,xnxq,xh,testingBean.getJSH()
                    ,testingBean.getKCH(),pjly,initIdArray(),initStarArray(),testingBean.getSKJC(),testingBean.getSKXQ(),skzc,testingBean.getSKDD());
        }else{
            ToastUtils.showShort(EvaluateClassRoomActivity.this,"评价内容不能为空");
        }
    }

    /**
     * 获取id数值集合
     */
    public String initIdArray(){
        String id = "";
        for (int i = 0; i < list.size(); i++) {
            if ((i+1)==list.size())
                id += list.get(i).getId()+"";
            else
                id += list.get(i).getId()+",";
        }
        return id;
    }

    /**
     * 获取星星数值集合
     */
    public String initStarArray(){
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            View rvView = layoutManager.findViewByPosition(i);    //2为recyclerView中item位置，
            //获取父类视图
            AutoRelativeLayout layout = (AutoRelativeLayout) rvView;
            //获取子控件
            ScaleRatingBar score = (ScaleRatingBar) layout.findViewById(R.id.item_evaluate_score);
            float currentRating = score.getRating();
            if ((i+1)==list.size())
                str += ((int) currentRating)+"";
            else
                str += ((int) currentRating)+",";
        }
        return str;
    }
}
